package com.desaysv.wandroid.utils;

import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jzl
 * @date 2022-11-10
 * @since 2022-11-10
 * @version 1.0
 */
public class StrUtils {
    /**
       * 字符串是否包含中文
       *
       * @param str 待校验字符串
       * @return true 包含中文字符 false 不包含中文字符
       */
  public static boolean isContainChinese(String str) {
        if (TextUtils.isEmpty(str)) {
          return false;
        }
        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]");
        Matcher m = p.matcher(str);
      return m.find();
  }

    /**
     * 是否包含特殊字符串
     * @param str 识别的目标字符串
     * @return true/false
     */
    public static boolean isSpecialChar(String str) {
        String regEx = "[ `~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }
    //随即字符串示例
    public static void main(String[] args) {
//        System.out.println(StrUtils.getRandom(24, TYPE.LETTER_CAPITAL_NUMBER_SIGN));
        System.out.println(getRandom(32, StrUtils.TYPE.LETTER_CAPITAL_NUMBER));
        System.out.println(getRandom(32, StrUtils.TYPE.LETTER_CAPITAL_NUMBER));
        System.out.println(getRandom(32, StrUtils.TYPE.LETTER_CAPITAL_NUMBER));
        System.out.println(getRandom(32, StrUtils.TYPE.LETTER_CAPITAL_NUMBER));
        System.out.println(getRandom(32, StrUtils.TYPE.LETTER_CAPITAL_NUMBER));
    }
    /***************随即字符串相关****************/
    private static final String[] LOWER_CASE = {
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    private static final String[] CAPITAL = {
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private static final String[] NUMBER = {
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    private static final String[] SIGN = {
            "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "`", "-", "=",
            "{", "}", "|", ":", "\"", "<", ">", "?",
            "[", "]", "\\", ";", "'", ",", ".", "/"};
    /**
     * 静态随机数
     */
    private static final Random RANDOM = new Random();
    /**
     * 获取随机组合码
     *
     * @param num  位数
     * @param type 类型
     *             <br>字符型 LETTER,
     *             <br>大写字符型 CAPITAL,
     *             <br>数字型 NUMBER,
     *             <br>符号型 SIGN,
     *             <br>大+小字符型 LETTER_CAPITAL,
     *             <br>小字符+数字 型 LETTER_NUMBER,
     *             <br>大+小字符+数字 型 LETTER_CAPITAL_NUMBER,
     *             <br>大+小字符+数字+符号 型 LETTER_CAPITAL_NUMBER_SIGN
     */
    public static String getRandom(int num, TYPE type) {
        ArrayList<String> temp = new ArrayList<>();
        if (type == TYPE.LETTER) {
            temp.addAll(Arrays.asList(LOWER_CASE));
        } else if (type == TYPE.CAPITAL) {
            temp.addAll(Arrays.asList(CAPITAL));
        } else if (type == TYPE.NUMBER) {
            temp.addAll(Arrays.asList(NUMBER));
        } else if (type == TYPE.SIGN) {
            temp.addAll(Arrays.asList(SIGN));
        } else if (type == TYPE.LETTER_CAPITAL) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(CAPITAL));
        } else if (type == TYPE.LETTER_NUMBER) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(NUMBER));
        } else if (type == TYPE.LETTER_CAPITAL_NUMBER) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(CAPITAL));
            temp.addAll(Arrays.asList(NUMBER));
        } else if (type == TYPE.LETTER_CAPITAL_NUMBER_SIGN) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(CAPITAL));
            temp.addAll(Arrays.asList(NUMBER));
            temp.addAll(Arrays.asList(SIGN));
        }
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < num; i++) {
            code.append(temp.get(RANDOM.nextInt(temp.size())));
        }
        return code.toString();
    }

    /**
     * 密码类型枚举
     *
     * @author ASUS
     */
    public enum TYPE {
        /**
         * 字符型
         */
        LETTER,
        /**
         * 大写字符型
         */
        CAPITAL,
        /**
         * 数字型
         */
        NUMBER,
        /**
         * 符号型
         */
        SIGN,
        /**
         * 大+小字符 型
         */
        LETTER_CAPITAL,
        /**
         * 小字符+数字 型
         */
        LETTER_NUMBER,
        /**
         * 大+小字符+数字 型
         */
        LETTER_CAPITAL_NUMBER,
        /**
         * 大+小字符+数字+符号 型
         */
        LETTER_CAPITAL_NUMBER_SIGN
    }
}
